﻿using STMS.BLL;
using STMS.Models.DModels;
using STMS.WSTMSystem.FModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.WSTMSystem.Product
{
    public partial class FrmProductList : Form
    {
        public FrmProductList()
        {
            InitializeComponent();
        }

        ProductBLL productBLL = new ProductBLL();
        int fIndex = 0;
        /// <summary>
        /// 产品入库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProductInStore_Click(object sender, EventArgs e)
        {
            FrmProductInStore fProInStore = new FrmProductInStore();
            Panel panel = this.Parent as Panel;
            panel.AddPanelPage(fProInStore);
        }

        private void FrmProductList1_Load(object sender, EventArgs e)
        {
            chkShowDel.Checked = false;
            FindProductList();
        }

        private void FindProductList()
        {
            string keywords = txtKeyWords.Text.Trim();
            bool showDel = chkShowDel.Checked;
            List<ProductInfo> proList = productBLL.GetProductList(keywords, showDel);
            if (proList.Count > 0)
            {
                dgvProductList.AutoGenerateColumns = false;
                dgvProductList.DataSource = proList;
                SetBtnsEnable(true);
                dgvProductList.ShowDgvBtnCols(showDel);
            }
            else
            {
                dgvProductList.DataSource = null;
                SetBtnsEnable(false);
            }
        }

        private void SetBtnsEnable(bool bl)
        {
            btnFind.Enabled = bl;
            btnDelete.Enabled = bl;
            dgvProductList.Visible = bl;
        }

        /// <summary>
        /// 打开仓库信息页
        /// </summary>
        /// <param name="actType"></param>
        /// <param name="storeId"></param>
        private void ShowProductInfoPage(int actType, int productId)
        {
            FrmProductInfo fProduct = new FrmProductInfo();
            fProduct.Tag = new FInfoData()
            {
                ActType = actType,
                FId = productId
            };
            fProduct.ReloadProductList += FindProductList;
            fProduct.ShowDialog();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            ShowProductInfoPage(1, 0);
        }

        private void dgvProductList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                DataGridViewCell curCell = dgvProductList.Rows[e.RowIndex].Cells[e.ColumnIndex];
                ProductInfo product = dgvProductList.Rows[e.RowIndex].DataBoundItem as ProductInfo;
                switch (curCell.FormattedValue.ToString())
                {
                    case "修改":
                        ShowProductInfoPage(2, product.ProductId);
                        break;
                    case "删除":
                        DeleteProduct(product, 1);
                        break;
                    case "恢复":
                        DeleteProduct(product, 0);
                        break;
                    case "移除":
                        DeleteProduct(product, 2);
                        break;
                    default: break;
                }
            }
        }

        /// <summary>
        /// 删除/恢复/移除 产品信息
        /// </summary>
        /// <param name="product"></param>
        /// <param name="delType"></param>
        private void DeleteProduct(ProductInfo product, int delType)
        {
            string delTypeName = "";
            string infoName = "产品";
            switch (delType)
            {
                case 1: delTypeName = "删除"; break;
                case 0: delTypeName = "恢复"; break;
                case 2: delTypeName = "移除"; break;
            }
            string msgTitle = $"{delTypeName}{infoName}";
            if (MsgBoxHelper.MsgBoxConfirm(msgTitle, $"您确定要{delTypeName}该{infoName}信息吗？") == DialogResult.Yes)
            {
                //删除
                bool bl = false;
                switch (delType)
                {
                    case 1:
                        int re = productBLL.LogicDeleteProduct(product.ProductId);
                        if (re == 2)
                        {
                            MsgBoxHelper.MsgErrorShow(msgTitle, $"{infoName}:{product.ProductName} 已有库存，不能删除！");
                            return;
                        }
                        else if (re == 1)
                            bl = true;
                        else
                            bl = false;
                        break;
                    case 0:
                        bl = productBLL.RecoverProduct(product.ProductId);
                        break;
                    case 2:
                        bl = productBLL.DeleteProduct(product.ProductId);
                        break;
                }
                string sucText = bl ? "成功" : "失败";
                string msg = $"{infoName}：{product.ProductName} {delTypeName}{sucText}！";
                if (bl)
                {
                    MsgBoxHelper.MsgBoxShow(msgTitle, msg);
                    FindProductList();
                }
                else
                {
                    MsgBoxHelper.MsgErrorShow(msgTitle, msg);
                    return;
                }
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            FindProductList();
        }

        private void chkShowDel_CheckedChanged(object sender, EventArgs e)
        {
            FindProductList();
        }

        /// <summary>
        /// 删除产品  可多删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<int> productIds = new List<int>();
            foreach (DataGridViewRow row in dgvProductList.Rows)
            {
                ProductInfo product = row.DataBoundItem as ProductInfo;
                DataGridViewCheckBoxCell chkCell = row.Cells["colChk"] as DataGridViewCheckBoxCell;
                if (chkCell.FormattedValue.ToString() == "True")
                {
                    productIds.Add(product.ProductId);
                }
            }
            string msgTitle = "删除产品";
            if (productIds.Count > 0)
            {
                if (MsgBoxHelper.MsgBoxConfirm(msgTitle, "你确定要删除选择的产品信息吗？") == DialogResult.Yes)
                {
                    string reDel = productBLL.LogicDeleteProducts(productIds);
                    if (reDel != "" && reDel != "0")
                    {
                        MsgBoxHelper.MsgErrorShow(msgTitle, $"选择的产品已有库存，它们的编号是：{reDel}");
                        return;
                    }
                    else if (reDel == "")
                    {
                        MsgBoxHelper.MsgBoxShow(msgTitle, $"选择的产品删除成功！");
                        FindProductList();
                    }
                    else
                    {
                        MsgBoxHelper.MsgErrorShow(msgTitle, $"选择的产品删除失败！");
                        return;
                    }
                }
            }
            else
            {
                MsgBoxHelper.MsgErrorShow("删除产品", "请选择要删除的产品信息！");
                return;
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            FrmProductList1_Load(this, e);
        }
    }
}
